//
//  LZMainController.swift
//  Weibo
//
//  Created by lzing on 16/3/19.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit
import SVProgressHUD

class LZMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let newTabBar = LZMainTabBar()
        // 设置tabBar
        self.setValue(newTabBar, forKey: "tabBar")
        // 设置加号按钮闭包
        newTabBar.composeButtonDidClickCallBack = { [weak self] () -> () in
            // 判断用户是否登录
            if !LZUserAccountViewModel.sharedUserAccountViewModel.userLoginStatus {
                SVProgressHUD.showErrorWithStatus("没有登录不能发微博")
                return
            }
            // 根据字符串创建字符串
            let productName = NSBundle.mainBundle().infoDictionary!["CFBundleName"] as? String ?? ""
            let className = NSClassFromString("\(productName).LZComposeController") as! UIViewController.Type
            let controller = className.init()
            // 弹出控制器
            self?.presentViewController(UINavigationController(rootViewController: controller), animated: true, completion: nil)
        
        }
        // 添加控制器
        addControllers()
    }
    
    /**
     添加子控制器
     */
    private func addControllers() {
        let homeVC = LZHomeViewController()
        addChildViewController(homeVC, title: "主页", imageName: "tabbar_home")
        
        let messageVC = LZMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        
        let discoverVC = LZDiscoverViewController()
        addChildViewController(discoverVC, title: "发现", imageName: "tabbar_discover")
        
        let profileVC = LZProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
    }
    
    @objc private func addChildViewController(controller: UIViewController, title: String, imageName: String){
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        let navHomeVC = UINavigationController(rootViewController: controller)
        addChildViewController(navHomeVC)
    }
    
}
